<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of convBox</title>
  <meta name="keywords" content="convBox">
  <meta name="description" content="Extremely fast 2D image convolution with a box filter.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<!-- menu.html channels -->
<h1>convBox
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>Extremely fast 2D image convolution with a box filter.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function J = convBox( I, r, s, nomex ) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment"> Extremely fast 2D image convolution with a box filter.

 Convolves an image by a F=ones(2*r+1,2*r+1)/(2*r+1)^2 filter. The
 convolution can be performed in constant time per-pixel, independent of
 the radius r. In fact the implementation is nearly optimal, with the
 convolution taking only slightly more time than creating a copy of the
 input array. Boundary effects are handled as if the image were padded
 symmetrically prior to performing the convolution. An optional integer
 downsampling parameter &quot;s&quot; can be specified, in which case the output is
 downsampled by s (the implementation is efficient with downsampling
 occurring simultaneously with smoothing, saving additional time).

 The output is exactly equivalent to the following Matlab operations:
  f = ones(1,2*r+1); f=f/sum(f);
  J = padarray(I,[r r],'symmetric','both');
  J = convn(convn(J,f,'valid'),f','valid');
  if(s&gt;1), t=floor(s/2)+1; J=J(t:s:end-s+t,t:s:end-s+t,:); end
 The computation, however, is an order of magnitude faster than the above.

 When used as a smoothing filter, the standard deviation (sigma) of a box
 filter with radius r can be computed using [sigma=sqrt(r*(r+1)/3)]. For
 the first few values of r this translates to: r=1: sigma=sqrt(2/3), r=2:
 sigma=sqrt(2), r=3: sigma=2. Given sigma, the equivalent value of r can
 be computed via [r=sqrt(12*sigma*sigma+1)/2-.5].

 The related function convTri performs convolution with a triangle filter,
 which has nicer properties if used for smoothing, but is slightly slower.

 This code requires SSE2 to compile and run (most modern Intel and AMD
 processors support SSE2). Please see: http://en.wikipedia.org/wiki/SSE2.

 USAGE
  J = convBox( I, r, [s], [nomex] )

 INPUTS
  I      - [hxwxk] input k channel single image
  r      - integer filter radius
  s      - [1] integer downsampling amount after convolving
  nomex  - [0] if true perform computation in matlab (for testing/timing)

 OUTPUTS
  J      - [hxwxk] smoothed image

 EXAMPLE
  I = single(imResample(imread('cameraman.tif'),[480 640]))/255;
  r = 5; s = 2; % set parameters as desired
  tic, J1=convBox(I,r,s); toc % mex version (fast)
  tic, J2=convBox(I,r,s,1); toc % matlab version (slow)
  figure(1); im(J1); figure(2); im(abs(J2-J1));

 See also conv2, <a href="convTri.html" class="code" title="function J = convTri( I, r, s, nomex )">convTri</a>

 Piotr's Computer Vision Matlab Toolbox      Version 3.02
 Copyright 2014 Piotr Dollar &amp; Ron Appel.  [pdollar-at-gmail.com]
 Licensed under the Simplified BSD License [see external/bsd.txt]</pre></div>





<!-- Start of Google Analytics Code -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4884268-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<!-- end of Google Analytics Code -->

<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2003</address>
</body>
</html>
